package org.example.netty.handler.channel;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import org.example.netty.model.CustomMessage;

/**
 * @author:hzc
 * @date:2025/5/23
 * @desc: 自定义协议编码器
 */
public class CustomProtocolEncoder extends MessageToByteEncoder<CustomMessage> {
    @Override
    protected void encode(ChannelHandlerContext ctx, CustomMessage msg, ByteBuf out) throws Exception {
        out.writeShort(msg.getMagic());
        out.writeByte(msg.getVersion());
        out.writeByte(msg.getSerializeAlgo());
        out.writeInt(msg.getReserved());
        out.writeByte(msg.getType());
        out.writeLong(msg.getRequestId());
        out.writeInt(msg.getLength());
        out.writeBytes(msg.getContent());

    }
}
